<?php
namespace Center\Controller;
use Think\Controller;
/**
 * 数据初始化更新处理控制器 DataController.class.php
 */
class DataController extends Controller{
    /* 属性声明 */
    //子集结构文件
    private $item_file = "./Org/Data/init_subitem.json";
    //字典结构文件
    private $dictionary_file = "./Org/Data/init_dic.json";
    //初始化顶级菜单文件
    private $original_top_list_file = "./Org/Data/Original_top_list.json";
    //初始化子集数据文件
    private $original_name_submodel = "./Org/Data/Original_name_submodel.json";
    //初始化省份数据
    private $province_file = "./Org/Data/Original_Province.json";
    //初始化市区数据
    private $city_file = "./Org/Data/Original_City.json";
    
    /* 方法声明 */
    /**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        //初始化连接redis数据库
        $this -> redis = connectRedis();
    }
    
    /**
     * 初始化子集数据结构 字典文件
     */
    public function init_item(){
        //读取redis最新结构文件
        $this -> redis -> select(0);
        //更新字典文件
        if($dic_data = $this -> redis -> get('init_dic')){
            //为字典添加标记数据
            $data1 = json_decode($dic_data,true);
            $data = $data1['CASC.HR.DICT.ALL'];
            foreach($data as $k => $v){
                $item_temp = $v['subitem'];
                foreach($item_temp as $k2 => $v2){
                    $item_temp[$k2]['display'] = true;
                }
                $data[$k]['subitem'] = $item_temp;
            }
            $temp['CASC.HR.DICT.ALL'] = $data;
            file_put_contents($this -> dictionary_file,json_encode($temp));
            //生成省市字典信息
            $this -> initProvince();
            $this -> initCity();
        }
        
        //更新人员数据结构
        if($json_data = json_decode($this -> redis -> get('init_subitem'),true)){
            file_put_contents($this -> item_file,json_encode($json_data));
            //自动初始化默认顶级子集集合
            $this -> initTopList($json_data);
            $this -> initSubitem($json_data);
        }
    }

    /**
     * 初始化默认顶级子集集合
     */
    private function initTopList($data){
        $data = $data['information'];
        //初始化数据缓冲区
        $buffer = [];
        foreach($data as $k => $v){
            $buffer[$k]['caption'] = $v['caption'];
            $buffer[$k]['name'] = $v['name'];
        }
        file_put_contents($this->original_top_list_file,json_encode($buffer));
    }
    
    /**
     * 初始化默认子集数据
     */
    private function initSubitem($data){
        $data = $data['information'];
        file_put_contents($this->original_name_submodel,json_encode($data));
    }
    
    /**
     * 初始化省份json结构
     */
    public function initProvince(){
        $dic_data = 
        json_decode(file_get_contents($this->dictionary_file),true)['CASC.HR.DICT.ALL'];
        $pro_data = null;
        foreach($dic_data as $k => $v){
            if($v['name']=='GB_ADDR'){
                $pro_data = $v;
                break;
            }
        }
        $pro_data = $pro_data['subitem'];
        $buf_list = [];
        foreach($pro_data as $k => $v){
            if(strlen($v['value'])<=2){
                $v['display'] = true;
                $buf_list[] = $v;
            }
        }
        //生成原始省份json文件
        file_put_contents($this -> province_file,  json_encode($buf_list));
    }
    
    /**
     * 初始化市区json结构
     */
    public function initCity(){
        $filter_list = ['天津市','北京市','上海市','重庆市'];
        $dic_data = 
        json_decode(file_get_contents($this->dictionary_file),true)['CASC.HR.DICT.ALL'];
        $pro_data = null;
        foreach($dic_data as $k => $v){
            if($v['name']=='GB_ADDR'){
                $pro_data = $v;
                break;
            }
        }
        $pro_data = $pro_data['subitem'];
        $buf_list = [];
        //获取所有省份数据
        $pro_list = json_decode(file_get_contents($this->province_file),true);
        //遍历所有省份
        foreach($pro_list as $k => $v){
            $pro_name = $v['caption'];
            $regex = '/^'.$v['value'].'/';
            $city_list = [];
            //直辖市情况处理
            if(in_array($pro_name,$filter_list)){
                $pro_list[$k]['city_list'] = [$v];
                continue;
            }
            //临时市区去重数据
            $temp = [];
            //遍历所有数据获取市区集合
            foreach($pro_data as $k2 => $v2){
                if(preg_match($regex, $v2['value'])==1){
                    if($v2['value']==$v['value']){
                        continue;
                    }
                    //去除省份字符串
                    $v2['caption'] = str_replace($pro_name,'',$v2['caption']);
                    //市区数据截取
                    $c_name = $v2['caption'];
                    if(($offset = strpos($c_name,'市'))!==false){
                        $c_name = substr($c_name,0,($offset+3));
                        $v2['caption'] = $c_name;
                        if(!in_array($c_name, $temp)){
                            $temp[] = $c_name;
                            $city_list[] = $v2;
                        }
                    }
                }
            }
            //收集数据
            $pro_list[$k]['city_list'] =  $city_list;
        }
        //生成市区文件
        file_put_contents($this->city_file,  json_encode($pro_list));
    }
    
    /**
     * 初始化redis结构数据
     */
    public function init_redis(){
        $file1 = "./Org/Data/temp/init_dic.json";
        $data1 = file_get_contents($file1);
        //测试更新字典操作
        //无相关操作
        
        
        
        $this -> redis -> select(0);
        $this -> redis -> set('init_dic',$data1);
        $file2 = "./Org/Data/temp/init_subitem.json";
        $data2 = file_get_contents($file2);
        
        $data2 = json_decode($data2,true);
        dump($data2);die;
        /*'sublist' => 
            array (size=122)
              ...
          'subvalue' => 
            array (size=0)
              ...
          'name' => string 'BO_CASC_HR_YGRS' (length=15)
          'caption' => string '员工入司' (length=12)
          'type' => string 'TABLE' (length=5)
         */
        $tt = ['sublist'=>[],'subvalue'=>[],'name'=>'haha','caption'=>'哈哈','type'=>'TABLE'];
        $data2['information'][] = $tt;
        $data2 = json_encode($tt);
        $this -> redis -> set('init_subitem',$data2);
    }
}